package Manage;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogRecorderAndAnalyzer {
    private static final String LOG_FILE = "作业日志.log";

    // 记录日志
    public void recordLog(String message) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_FILE, true))) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timestamp = dateFormat.format(new Date());
            writer.write(timestamp + " - " + message);
            writer.newLine();
            System.out.println("日志已记录");
        } catch (IOException e) {
            System.out.println("日志记录失败: " + e.getMessage());
        }
    }

    // 分析日志
    public void analyzeLogs() {
        try (BufferedReader reader = new BufferedReader(new FileReader(LOG_FILE))) {
            String line;
            int logCount = 0;
            while ((line = reader.readLine()) != null) {
                logCount++;
                System.out.println(line);
            }
            System.out.println("总日志数量: " + logCount);
        } catch (IOException e) {
            System.out.println("日志分析失败: " + e.getMessage());
        }
    }
}
